package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.common.R;
import com.example.demo.common.RPage;
import com.example.demo.entity.allTitles.*;
import com.example.demo.mapper.AllTitlesMapper;
import com.example.demo.service.AllTitlesService;
import com.example.demo.service.baseContext.BaseContextService;
import com.example.demo.utils.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class AllTitlesServiceImpl extends ServiceImpl<AllTitlesMapper, AllTitles> implements AllTitlesService {
    @Autowired
    private BaseContextService bcs;

    @Override
    public R<RPage<AllTitles>> selAllTitles(AllTitlesSelList param) {
        LambdaQueryWrapper<AllTitles> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(ObjectUtils.isValid(param.getTitle()), AllTitles::getTitle, param.getTitle())
                .eq(AllTitles::getCreateBy, bcs.getUserData().getUserId());
        Page<AllTitles> page = new Page<>(param.getPageNum(), param.getPageSize());
        Page<AllTitles> page1 = baseMapper.selectPage(page, wrapper);
        RPage<AllTitles> rPage = RPage.of(page1);
        return R.ok(rPage);
    }

    @Override
    public R<List<AllTitles>> selAllAllTitles(AllTitlesSelAll param) {
        LambdaQueryWrapper<AllTitles> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(ObjectUtils.isValid(param.getTitle()), AllTitles::getTitle, param.getTitle())
                .eq(AllTitles::getCreateBy, bcs.getUserData().getUserId());
        wrapper.orderByDesc(AllTitles::getCreateTime);
        List<AllTitles> list = baseMapper.selectList(wrapper);
        return R.ok(list);
    }

    @Override
    public R<List<AllTitles>> selOnesAllTitles(Integer[] ids) {
        List<AllTitles> list = baseMapper.selectByIds(Arrays.asList(ids));
        return R.ok(list);
    }

    @Override
    public R<AllTitles> selOneAllTitles(Integer id) {
        AllTitles data = baseMapper.selectById(id);
        return R.ok(data);
    }

    @Override
    public R<Boolean> insAllTitles(AllTitlesInsOne allTitlesInsOne) {
        AllTitles allTitles = new AllTitles();
        BeanUtils.copyProperties(allTitlesInsOne, allTitles);
        baseMapper.insert(allTitles);
        return R.ok(true);
    }

    @Override
    public R<Boolean> insAllTitless(AllTitlesInsOne[] allTitlesInsOnes) {
        for (AllTitlesInsOne allTitlesInsOne : allTitlesInsOnes) {
            AllTitles allTitles = new AllTitles();
            BeanUtils.copyProperties(allTitlesInsOne, allTitles);
            baseMapper.insert(allTitles);
        }
        return R.ok(true);
    }

    @Override
    public R<Boolean> updAllTitles(AllTitlesUpdOne allTitlesUpdOne) {
        AllTitles allTitles = new AllTitles();
        BeanUtils.copyProperties(allTitlesUpdOne, allTitles);
        baseMapper.updateById(allTitles);
        return R.ok(true);
    }

    @Override
    public R<Boolean> updAllTitless(AllTitlesUpdOne[] allTitlesUpdOnes) {
        for (AllTitlesUpdOne allTitlesUpdOne : allTitlesUpdOnes) {
            AllTitles allTitles = new AllTitles();
            BeanUtils.copyProperties(allTitlesUpdOne, allTitles);
            baseMapper.updateById(allTitles);
        }
        return R.ok(true);
    }

    @Override
    public R<Boolean> delAllTitles(Integer[] ids) {
        baseMapper.deleteByIds(Arrays.asList(ids));
        return R.ok(true);
    }
}
